import axios from 'axios'
import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLList,
  GraphQLInt,
  GraphQLFloat,
  GraphQLNonNull
} from 'graphql';
let MessageType = new GraphQLObjectType({
  name: 'MessageType',
  fields: {
    message: {
      type: GraphQLString
    }
  }
})

let CommentType = new GraphQLObjectType({
  name: 'CommentType',
  fields: {
    id: {
      type: GraphQLInt
    },
    subject: {
      type: GraphQLInt
    },
    theaters: {
      type: GraphQLInt
    },
    content: {
      type: GraphQLString
    },
    type: {
      type: GraphQLString
    }
  }
})

let TheaterType = new GraphQLObjectType({
  name: 'TheaterType',
  fields: {
    id: {
      type: GraphQLInt
    },
    tag: {
      type: GraphQLString
    },
    adIndex: {
      type: GraphQLInt,
    },
    header: {
      type: new GraphQLObjectType({
        name: 'header',
        fields: {
          id: {
            type: GraphQLInt
          },
          textAlign: {
            type: GraphQLString
          },
          actionUrl: {
            type: GraphQLString
          },
          icon: {
            type: GraphQLString
          },
          description: {
            type: GraphQLString
          },
          title: {
            type: GraphQLString
          },
          iconType: {
            type: GraphQLString
          }
        }
      })
    },
    comments: {
      type: new GraphQLList(CommentType),
      resolve(obj, args, context) {
        // console.log('obj',obj)
        // 可以根据id查询数据
        return axios({
            url: 'http://localhost:53000/comments?theaters=' +obj.id
          }).then(res => {
            console.log(res)
            return res.data
          })
      },
    }
   
    
  }
})

// 作用是接口整合
let movieType = new GraphQLObjectType({
  name: 'movieType',
  fields: {
    id: {
      type: GraphQLInt
    },
    title: {
      type: GraphQLString
    },
    genres: {
      type: GraphQLString
    },
    type: {
      type: GraphQLString
    },
    adIndex: {
      type: GraphQLInt
    },
    data: {
      type: new GraphQLObjectType({
        name: 'dataQueryType',
        fields: {
          dataType: {
            type: GraphQLString
          }
        }
      })
    },
    theaters: {
      type: TheaterType ,
      resolve(obj, args, context) {
        // console.log('obj',obj)
        // 可以根据id查询数据
        return axios({
            url: 'http://localhost:53000/theaters/'+obj.id
          }).then(res => {
            // console.log(res)
            return res.data
          })
      },
    },
    comments: {
      type: new GraphQLList(CommentType),
      resolve(obj, args, context) {
        console.log('obj',obj)
        // 可以根据id查询数据
        return axios({
            url: 'http://localhost:53000/comments?subject=' +obj.id
          }).then(res => {
            console.log(res)
            return res.data
          })
      },
    }
    
  }
})

var schema = new GraphQLSchema({
  // 查询
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      // 字段
      /* 
        调试方式
        query demo {
          hello
        } 
      */
      hello: {
        type: GraphQLString, // 字符串类型
        resolve() {
          return 'world';
        },
      },
      // 字段
      movies: { 
        type: new GraphQLList(movieType), // 数组类型
        resolve() {
          return axios({
              url: 'http://localhost:53000/subjects'
            }).then(res => {
              return res.data
            })
        },
      },
      theaters: {
        type: new GraphQLList(TheaterType) ,
        resolve(obj, args, context) {
          console.log('obj',obj)
          return axios({
              url: 'http://localhost:53000/theaters/1'
            }).then(res => {
              // console.log(res)
              return res.data
            })
        },
      }
    },
  }),
  // 添加、编辑、删除等操作
  
  mutation: new GraphQLObjectType({
    name: 'RootMutationType',
    fields: {
      // 添加
      /* 
        mutation movies {
          insert(title: "十万分之二", genres: "动作片", rating: 7.2, theater: 3) {
            title
          }
        }
      */
      insert: {
        type: movieType,
        args: { 
            title: {
              // GraphQLNonNull 非空类型
              type: new GraphQLNonNull(GraphQLString)
            },
            genres:{
              type: GraphQLString
            },
            rating:{
              type: GraphQLFloat
            },
            theater:{
              type: GraphQLInt
            }
        },
        resolve(obj, args) {
          // console.log(args)
          // let {title, genres, rating, theater} = args
          return axios.post('http://localhost:9000/subjects',{...args}).then(result => result.data)
          /* axios({
            url: 'http://localhost:9000/subjects',
            method: 'POST',
            data: {
              title, genres, rating, theater
            }
          }).then(result => {
            console.log(result)
            return result.data
          }) */
        }
      },
      // 修改
      /* 
        mutation movies {
          update_put(title: "十万分之二", genres: "动作片", rating: 7.2, theater: 3) {
            title
          }
        }
      */
      update_put: {
        type: movieType,
        args: { 
            id: {
              type: new GraphQLNonNull(GraphQLInt) 
            },
            title: {
              // GraphQLNonNull 非空类型
              type: new GraphQLNonNull(GraphQLString)
            },
            genres:{
              type: GraphQLString
            },
            rating:{
              type: GraphQLFloat
            },
            theater:{
              type: GraphQLInt
            }
        },
        resolve(obj, args) {
          console.log(args.id)
          // let {title, genres, rating, theater} = args
          return axios.put('http://localhost:9000/subjects/'+args.id,{...args})
          .then(result => result.data)
          /* axios({
            url: 'http://localhost:9000/subjects',
            method: 'POST',
            data: {
              title, genres, rating, theater
            }
          }).then(result => {
            console.log(result)
            return result.data
          }) */
        }
      },
      // 只改部分
      update_patch: {
        type: movieType,
        args: { 
            id: {
              type: new GraphQLNonNull(GraphQLInt) 
            },
            title: {
              // GraphQLNonNull 非空类型
              type: new GraphQLNonNull(GraphQLString)
            }
        },
        resolve(obj, args) {
          // let {title, genres, rating, theater} = args
          return axios.patch('http://localhost:9000/subjects/'+args.id,{...args})
          .then(result => result.data)
          /* axios({
            url: 'http://localhost:9000/subjects',
            method: 'POST',
            data: {
              title, genres, rating, theater
            }
          }).then(result => {
            console.log(result)
            return result.data
          }) */
        }
      },      
      update_delete: {
        type: MessageType,
        args: { 
            id: {
              type: new GraphQLNonNull(GraphQLInt) 
            }
        },
        resolve(obj, args) {
          console.log(args.id)
          // let {title, genres, rating, theater} = args
          return axios.delete('http://localhost:9000/subjects/'+args.id)
          .then(result => ({message: '删除数据成功！'}))
        }
      }
    }
  }) 
});

export default schema